import Taro from "@tarojs/taro";
import {doLogin} from "@/app/utils/login";
import {host, noLog} from "@/app/configs";
import {navigateTo} from "./Taro";

const noConsole = noLog();

export async function post(url, params) {
  if (!noConsole) {
    console.log(
      `${new Date().toLocaleString()}【 M=${url} 】P=${JSON.stringify(params)}`
    );
  }
  const {statusCode, data: resData} = await Taro.request({
    url: `${host()}/${url}`,
    data: JSON.stringify(params),
    header: {
      "content-type": "application/json",
      token: Taro.getStorageSync("token"),
    },
    method: "POST",
  });
  if (statusCode != 200) {
    throw new Error(`网络请求错误，状态码${statusCode}`);
  }
  if (!noConsole) {
    console.log(
      `${new Date().toLocaleString()}【 M=${url} 】【接口响应：】`,
      resData
    );
  }
  const {msg, code, ...rest} = resData;
  if (code === 200) {
    return {...rest};
  }
  if (code === 201) {
    Taro.setStorageSync("token", msg);
    return post(url, params);
  }
  if (code === 401) {
    navigateTo("login");
  }
  if (code === 412) {
    navigateTo("login");
  }
  if (code === 413) {
    return {data: msg};
  }
  Taro.showToast({title: `${msg}~` || code, icon: "none", mask: true});
}
